function rois = click2rois2(s,nrois,hight,plotting)
%for defining a liner ROI by clicking on image
%function rois = click2rois1(s,nrois,hight)
%INPUT:     s = scan-structure such as read in by scanread.m
%       nrois = number of ROIs you would like to apply
%       hight = number of pixels the ROI should extent below and above the
%               defined line
%    plotting = plots the integrated scans
%OUTPUT: structure 'rois' containing:
%        -indices: matrix indices of the defined ROI (to be used to sum over for obtaining the spectrum)
%        -data:    integrated scan using rois.indices
%        -datan:   integrated scan using rois.indices normalized by monitor
%                  from Specfile
%        -datae:   error (poisson counting)
%        -dataen:  normalized error
%        -cenom:   center of mask (Tuomas: 'more usefull for position than gaussian fit')

try logscaling;
catch
    logscaling = 1;
end
try hight;
catch
    hight=5;
end

if nargin<4
    plotting = 0;
end

for n=1:nrois
    if logscaling
        imagesc(log(sumx(s.A)))
    else
        imagesc(sumx(s.A))
    end
    axis square
    title('Select start- and endpoint of linear ROI by clicking and press Return');
    fprintf('%i/%i: ROI, Select two points and press Return',n,nrois)
    getroi=ginput(2);
    line([getroi(:,1)],[getroi(:,2)]);
    roix=[getroi(1,1)-5:getroi(2,1)+5];
    roix=roix(find(roix>1 & roix<256));
    
    pause
    roiy=round(polyval(polyfit(getroi(:,1),getroi(:,2),1),roix));
    roix=round(roix);
    T = zeros(size(s.A,2),size(s.A,3));

    for i = 1:length(roix)
        for h =-hight:hight
            T(roiy(i)+h,roix(i)) = 1;
        end
    end
    rois(n).indices = find(T==1);
end    
    
%read in scan for instant plotting
for h=1:length(rois)
    for l=1:size(s.A,1)
        rois(h).data(l) = sumy(s.A(l,rois(h).indices));
        rois(h).datan(l) = sqrt(sumy(s.A(l,rois(h).indices)));
        rois(h).datae(l) = sumy(s.A(l,rois(h).indices))./s.monitor(l);
        rois(h).dataen(l) = sqrt(sumy(s.A(l,rois(h).indices)))./s.monitor(l);
    end
end

for n=1:length(rois)
    rois(n).cenom = trapz(s.energy_cc,rois(n).data'.*s.energy_cc)./trapz(s.energy_cc,rois(n).data');
end

if plotting
figure
    subplot(3,3,1)
        plot(s.energy_cc-rois(1).cenom,rois(1).datan)
        legend('ROI 1')
    subplot(3,3,2)
        plot(s.energy_cc-rois(2).cenom,rois(2).datan)
        legend('ROI 2')
    subplot(3,3,3)
        plot(s.energy_cc-rois(3).cenom,rois(3).datan)
        legend('ROI 3')
    subplot(3,3,4)
        plot(s.energy_cc-rois(4).cenom,rois(4).datan)
        legend('ROI 4')
    subplot(3,3,5)
        plot(s.energy_cc-rois(5).cenom,rois(5).datan)
        legend('ROI 5')
    subplot(3,3,6)
        plot(s.energy_cc-rois(6).cenom,rois(6).datan)
        legend('ROI 6')
    subplot(3,3,7)
        plot(s.energy_cc-rois(7).cenom,rois(7).datan)
        legend('ROI 7')
    subplot(3,3,8)
        plot(s.energy_cc-rois(8).cenom,rois(8).datan)
        legend('ROI 8')
    subplot(3,3,9)
        plot(s.energy_cc-rois(9).cenom,rois(9).datan)
        legend('ROI 9')
end